var o = require("../UIManager/UISoundManager.js");
cc.Class({
    extends: cc.Component,
    properties: {
        btnStart: {
            default: null,
            type: cc.Button
        },
        btnBack: {
            default: null,
            type: cc.Button
        },
        nodeWait: {
            default: null,
            type: cc.Node
        },
        nodeFriRoom: {
            default: null,
            type: cc.Node
        },
        roomTxt: {
            default: null,
            type: cc.Node
        },
        roomID: {
            default: null,
            type: cc.Label
        },
        tableDescList: {
            default: null,
            type: cc.Node
        },
        _timeTotal: 31
    },
    onLoad: function () {
        this.invite = Global.utils.searchNode(this.node, "invite"), Global.notificationCenter.on(Global.eventName.GAME_TABLE_GAME_START, this.onGameStart.bind(this), this),
            Global.notificationCenter.on(Global.eventName.MSG_JUDGE_SEAT_FULL, this.setWeChatActive.bind(this), this),
            Global.notificationCenter.on(Global.eventName.MSG_LEAVE, this.userLeave.bind(this), this),
            this.tableModel = Global.tableModel, this.nodeWait = Global.utils.searchNode(this.node, "nodeWait");
    },
    userLeave: function (e) {
        if (e.seatId == Global.config.SEAT_DOWN && !Global.tableModel.getIsShowTotalResult()) {
            var t = Global.tableModel.getRoomId(), n = Global.tableModel.getTableId(),
                o = Global.tableModel.getSeatId(), i = [t, n, o];
            if (Global.tableModel.getTableType() == Global.config.tableType.CREATE) {
                if (tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventType_LEAVE_FRI_ROOM, i),
                Global.tableModel.getSeatId() != Global.config.SEAT_DOWN) {
                    var a = {
                        text: "房主解散房间",
                        confirmText: "确定",
                        confirmSp: Global.spriteAtlas.getOKSpf(),
                        confirmCallback: function () {
                            console.log(" confirmCallback "), 0 == t && 0 != n || Global.msgHandler.requestTableLeave(t, n, o);
                            SceneHelper.loadScene("homeScene");
                        },
                        needCloseBtn: !1
                    };
                    Global.UIPopupManager.alert(a);
                }
            } else tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventType_LEAVE_ROOM, i);
        }
    },
    start: function () {
        this.tableModel.getTableType() == Global.config.tableType.CREATE ? (cc.sys.platform == cc.sys.WECHAT_GAME && Global.spriteAtlas.setSpriteFrame(this.invite.children[0].getComponent(cc.Sprite), "Texture/common", "wechat_invite"),
            this.setNodeWaitActive(!1), this.setSeatsActive(!0), this.refreshBtns(), this.refresh()) : (this.setNodeWaitActive(!0),
            this.setSeatsActive(!1)), Global.spriteAtlas.loadGameSpriteAtlas();
    },
    refreshBtns: function () {
        var e = 0 == Global.tableModel.getCurrentNumberOfGames();
        Global.tableModel.isReconnect() && (console.log("重新连接 === "), Global.tableModel.updatePlayerModel());
        var t = Global.tableModel.getPlayers().length == Global.config.PLAY_NUM,
            n = Global.tableModel.toLocalSeatIndex(Global.tableModel.getSeatId()),
            o = Global.tableModel.getPlayerModel(n);
        console.log("refreshBtns", o, o.getState()), Global.utils.searchNode(this.node, "btnBack").active = e,
            Global.utils.searchNode(this.node, "btn_ready").active = e && o.getState() != Global.config.playState.READY,
            this.invite.active = e && !t;
    },
    setWeChatActive: function (e) {
        console.log("setWeChatActive data =", e, this.invite), this.invite && (this.invite.active = !e || !(e.count == Global.config.PLAY_NUM));
    },
    setNodeWaitActive: function (e) {
        this.nodeWait.active = e;
    },
    setSeatsActive: function (e) {
        if (this.nodeFriRoom.active = e, e) {
            this.roomID.string = Global.tableModel.getCreateTableNo();
            var t = Global.tableModel.getCreateTableDescList();
            t.reverse();
            for (var n = 0; n < t.length; n++) {
                var o = this.getItem(n);
                o.active = !0, o.getComponent(cc.Label).string = t[n];
            }
        }
    },
    getItem: function (e) {
        var t = Global.utils.searchNode(this.tableDescList, "content");
        if (e < t.children.length) return t.children[e];
        var n = cc.instantiate(t.children[0]);
        return t && t.addChild(n), n;
    },
    refresh: function () {
        Global.notificationCenter.emit(Global.eventName.GAME_WAITTING_SHOWHEADDATA);
    },
    onGameStart: function () {
        if (!this._isLoadGame) {
            this._isLoadGame = !0, tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventType_TABLE_START, []),
                Global.notificationCenter.stop();
            SceneHelper.loadScene("gameScene", function () {
                Global.notificationCenter.start();
            });
        }
    },
    onClickStart: function () {
        Global.notificationCenter.stop();
        SceneHelper.loadScene("gameScene", function () {
            Global.notificationCenter.start(), Global.notificationCenter.emit(Global.eventName.GAME_REFRESH_STAND_TILE);
        });
    },
    onClickBack: function (e, t, n) {
        n || o.playEffect("select");
        var i = Global.tableModel.getRoomId(), a = Global.tableModel.getTableId(),
            r = Global.tableModel.getSeatId(), s = Global.tableModel.getTableType();
        0 == i && 0 != a || Global.msgHandler.requestTableLeave(i, a, r);
        var l = "selectScene";
        "create" == s && (l = "homeScene"), SceneHelper.loadScene(l);
    },
    onShareBtn: function (e) {
        o.playEffect("select"), console.log("onShareBtn");
        var t = {
            inviteToFriRoom: 1,
            createRoomID: Global.tableModel.getCreateTableNo()
        }, n = JSON.stringify(t), i = {
            share_type: Global.config.sharePointId.friRoomInvite
        };
        tywx.PropagateInterface.getWeightShareConfigInfoByTypeWithShare(i, n, function (e) {
            console.log("onShareBtn share succCallback");
        });
    },
    onReadyClick: function () {
        o.playEffect("select"), Global.msgHandler.requestReady(Global.tableModel.getRoomId(), Global.tableModel.getTableId(), Global.tableModel.getSeatId()),
            Global.utils.searchNode(this.node, "btn_ready").active = !1;
    },
    onSetButton: function () {
        this.waitForPlayer();
    },
    waitForPlayer: function () {
        this.btnStart.node.active = !1, this.nodeWait.active = !0;
    },
    update: function (e) {
        this.nodeWait.active && (this._timeTotal -= e, this._timeTotal <= 0 && (this._timeTotal = 0),
            Global.utils.searchComp(this.nodeWait, "time", cc.Label).string = "(" + parseInt(this._timeTotal) + ")",
        0 == this._timeTotal && this.onClickBack(null, null, !0)), Global.tableModel.getIsGameStart() && this.onGameStart();
    },
    onDestroy: function () {
        Global.notificationCenter.offAll(this);
    }
})
